home *** CD-ROM | disk | FTP | other *** search
/ JavaScript Source Code 2002 / JavaScript Source Code 2002.iso / games / slider-puzzle.js < prev    next >
Encoding:
JavaScript  |  2001-03-13  |  10.2 KB  |  343 lines

  1. // Dynapi code by Dan Steinman
  2. // http://www.dansteinman.com/dynapi/
  3.  
  4. // Name = new Puzzle(Xpos, Ypos, 'Name', width, height, 'Color', 'GifPath');
  5. // Name must be the same in both places GifPath may be relative or absolute
  6.  
  7. function Puzzle(q,v,n,W,H,c,g){
  8. this.mixem=null
  9. this.x=new Array()
  10. this.y=new Array()
  11. this.n=new Array()
  12. this.name=n
  13. this.f=false
  14. this.ma=false
  15. this.g=0
  16. this.w=W/3
  17. this.h=H/3
  18. s=css(n+"kDiv",q,v,W+20,H+60,c)
  19. s+=css(n+"mDiv",10,50,W,H,c)
  20. d='<DIV ID="'+n+'kDiv"><center><font color=blue size=5>'
  21. d+='<a href="javascript:'+n+'.scramble()">Scramble</a> or '
  22. d+='<a href="javascript:'+n+'.solve()">Solve</a></font></center>\n'
  23. d+='<DIV ID="'+n+'mDiv">\n'
  24. for(i=0;i<8;i++){
  25. m=n+i
  26. cn1=i
  27. cn2=0
  28. if(cn1>5)cn1-=6,cn2=2
  29. if(cn1>2)cn1-=3,cn2=1
  30. x=this.w*cn1
  31. y=this.h*cn2
  32. s+=css(m+"aDiv",x,y,this.w,this.h,c)
  33. s+=css(m+"bDiv",0-x,0-y,W,H)
  34. d+='<DIV ID="'+m+'aDiv" onclick="javascript:'+n+'.mov('+i+')">'
  35. d+='<DIV ID="'+m+'bDiv"><img src='+g+' height='+H+' width='+W+'>'
  36. d+='</div></div>\n'
  37. this.x[i]=cn1
  38. this.y[i]=cn2
  39. this.n[i]=m + "a"
  40. }
  41. d+='</div></div>\n'
  42. this.x[8]=2
  43. this.y[8]=2
  44. this.s=s
  45. this.d=d
  46. this.getCss  =PuzzleGetCss
  47. this.getDiv  =PuzzleGetDiv
  48. this.move    =PuzzleMove
  49. this.mov     =PuzzleMov  
  50. this.scramble=PuzzleSc  
  51. this.solve   =PuzzleSolve
  52. this.mixemup =PuzzleSC 
  53. this.stop    =PuzzleStop
  54. }
  55. function PuzzleMov(i){if(this.ma)return
  56. eval(this.name+'.move(i)')
  57. }
  58. function PuzzleStop(){
  59. eval('clearInterval('+this.mixem+')')
  60. this.ma=false
  61. }
  62. function PuzzleSc(){if(this.ma)return
  63. this.ma=true
  64. eval('setTimeout("'+this.name+'.stop()",15500)')
  65. eval('this.mixem=setInterval("'+this.name+'.mixemup()",300)')
  66. function PuzzleSC(){
  67. this.f=false
  68. while(!this.f){
  69. eval(this.name+'.move('+this.g+++')')
  70. if(this.g==8)this.g=-1}
  71. }
  72. function PuzzleSolve(){if(this.ma)return
  73. for(i=0;i<3;i++){for(j=0;j<3;j++){
  74. if(i*3+j<8)eval(this.n[i*3+j]+'.slideTo(this.w * j,this.h * i)')
  75. this.y[i*3+j]=i
  76. this.x[i*3+j]=j}}
  77. }     
  78. function PuzzleMove(n){
  79. x = this.x[n]
  80. y = this.y[n]
  81. w = this.x[8]
  82. z = this.y[8]
  83. if(((x==w)|(y==z))&(((x-1)==w)|((x+1)==w)|((y-1)==z)|((y+1)==z))){
  84. this.f=true
  85. eval(this.n[n]+'.slideTo(this.w*w,this.h*z)')
  86. this.x[n] = w
  87. this.y[n] = z
  88. this.x[8] = x
  89. this.y[8] = y}
  90. }
  91. function PuzzleGetCss(){return this.s}
  92. function PuzzleGetDiv(){return this.d}
  93. function init(){DynLayerInit()}
  94. // Dynamic Layer Object
  95. // sophisticated layer/element targeting and animation object which provides the core functionality needed in most DHTML applications
  96. // 19990604
  97.  
  98. // Copyright (C) 1999 Dan Steinman
  99. // Distributed under the terms of the GNU Library General Public License
  100. // Available at http://www.dansteinman.com/dynapi/
  101.  
  102. function DynLayer(id,nestref,frame) {
  103. if (!is.ns5 && !DynLayer.set && !frame) DynLayerInit()
  104. this.frame = frame || self
  105. if (is.ns) {
  106. if (is.ns4) {
  107. if (!frame) {
  108. if (!nestref) var nestref = DynLayer.nestRefArray[id]
  109. if (!DynLayerTest(id,nestref)) return
  110. this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
  111. }
  112. else this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id]
  113. this.elm = this.event = this.css
  114. this.doc = this.css.document
  115. }
  116. else if (is.ns5) {
  117. this.elm = document.getElementById(id)
  118. this.css = this.elm.style
  119. this.doc = document
  120. }
  121. this.x = this.css.left
  122. this.y = this.css.top
  123. this.w = this.css.clip.width
  124. this.h = this.css.clip.height
  125. }
  126. else if (is.ie) {
  127. this.elm = this.event = this.frame.document.all[id]
  128. this.css = this.frame.document.all[id].style
  129. this.doc = document
  130. this.x = this.elm.offsetLeft
  131. this.y = this.elm.offsetTop
  132. this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
  133. this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
  134. }
  135. this.id = id
  136. this.nestref = nestref
  137. this.obj = id + "DynLayer"
  138. eval(this.obj + "=this")
  139. }
  140. function DynLayerMoveTo(x,y) {
  141. if (x!=null) {
  142. this.x = x
  143. if (is.ns) this.css.left = this.x
  144. else this.css.pixelLeft = this.x
  145. }
  146. if (y!=null) {
  147. this.y = y
  148. if (is.ns) this.css.top = this.y
  149. else this.css.pixelTop = this.y
  150. }
  151. }
  152. function DynLayerMoveBy(x,y) {
  153. this.moveTo(this.x+x,this.y+y)
  154. }
  155. function DynLayerShow() {
  156. this.css.visibility = (is.ns4)? "show" : "visible"
  157. }
  158. function DynLayerHide() {
  159. this.css.visibility = (is.ns4)? "hide" : "hidden"
  160. }
  161. DynLayer.prototype.moveTo = DynLayerMoveTo
  162. DynLayer.prototype.moveBy = DynLayerMoveBy
  163. DynLayer.prototype.show = DynLayerShow
  164. DynLayer.prototype.hide = DynLayerHide
  165. DynLayerTest = new Function('return true')
  166.  
  167. // DynLayerInit Function
  168. function DynLayerInit(nestref) {
  169. if (!DynLayer.set) DynLayer.set = true
  170. if (is.ns) {
  171. if (nestref) ref = eval('document.'+nestref+'.document')
  172. else {nestref = ''; ref = document;}
  173. for (var i=0; i<ref.layers.length; i++) {
  174. var divname = ref.layers[i].name
  175. DynLayer.nestRefArray[divname] = nestref
  176. var index = divname.indexOf("Div")
  177. if (index > 0) {
  178. eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
  179. }
  180. if (ref.layers[i].document.layers.length > 0) {
  181. DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
  182. }
  183. }
  184. if (DynLayer.refArray.i < DynLayer.refArray.length) {
  185. DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
  186. }
  187. }
  188. else if (is.ie) {
  189. for (var i=0; i<document.all.tags("DIV").length; i++) {
  190. var divname = document.all.tags("DIV")[i].id
  191. var index = divname.indexOf("Div")
  192. if (index > 0) {
  193. eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
  194. }
  195. }
  196. }
  197. return true
  198. }
  199. DynLayer.nestRefArray = new Array()
  200. DynLayer.refArray = new Array()
  201. DynLayer.refArray.i = 0
  202. DynLayer.set = false
  203.  
  204. // Slide Methods
  205. function DynLayerSlideTo(endx,endy,inc,speed,fn) {
  206. if (endx==null) endx = this.x
  207. if (endy==null) endy = this.y
  208. var distx = endx-this.x
  209. var disty = endy-this.y
  210. this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  211. }
  212. function DynLayerSlideBy(distx,disty,inc,speed,fn) {
  213. var endx = this.x + distx
  214. var endy = this.y + disty
  215. this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  216. }
  217. function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
  218. if (this.slideActive) return
  219. if (!inc) inc = 20
  220. if (!speed) speed = 20
  221. var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
  222. if (num==0) return
  223. var dx = distx/num
  224. var dy = disty/num
  225. if (!fn) fn = null
  226. this.slideActive = true
  227. this.slide(dx,dy,endx,endy,num,1,speed,fn)
  228. }
  229. function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
  230. if (!this.slideActive) return
  231. if (i++ < num) {
  232. this.moveBy(dx,dy)
  233. this.onSlide()
  234. if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
  235. else this.onSlideEnd()
  236. }
  237. else {
  238. this.slideActive = false
  239. this.moveTo(endx,endy)
  240. this.onSlide()
  241. this.onSlideEnd()
  242. eval(fn)
  243. }
  244. }
  245. function DynLayerSlideInit() {}
  246. DynLayer.prototype.slideInit = DynLayerSlideInit
  247. DynLayer.prototype.slideTo = DynLayerSlideTo
  248. DynLayer.prototype.slideBy = DynLayerSlideBy
  249. DynLayer.prototype.slideStart = DynLayerSlideStart
  250. DynLayer.prototype.slide = DynLayerSlide
  251. DynLayer.prototype.onSlide = new Function()
  252. DynLayer.prototype.onSlideEnd = new Function()
  253.  
  254. // Clip Methods
  255. function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
  256. if (is.ie) {
  257. if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
  258. else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
  259. }
  260. }
  261. function DynLayerClipTo(t,r,b,l) {
  262. if (t==null) t = this.clipValues('t')
  263. if (r==null) r = this.clipValues('r')
  264. if (b==null) b = this.clipValues('b')
  265. if (l==null) l = this.clipValues('l')
  266. if (is.ns) {
  267. this.css.clip.top = t
  268. this.css.clip.right = r
  269. this.css.clip.bottom = b
  270. this.css.clip.left = l
  271. }
  272. else if (is.ie) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
  273. }
  274. function DynLayerClipBy(t,r,b,l) {
  275. this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
  276. }
  277. function DynLayerClipValues(which) {
  278. if (is.ie) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
  279. if (which=="t") return (is.ns)? this.css.clip.top : Number(clipv[0])
  280. if (which=="r") return (is.ns)? this.css.clip.right : Number(clipv[1])
  281. if (which=="b") return (is.ns)? this.css.clip.bottom : Number(clipv[2])
  282. if (which=="l") return (is.ns)? this.css.clip.left : Number(clipv[3])
  283. }
  284. DynLayer.prototype.clipInit = DynLayerClipInit
  285. DynLayer.prototype.clipTo = DynLayerClipTo
  286. DynLayer.prototype.clipBy = DynLayerClipBy
  287. DynLayer.prototype.clipValues = DynLayerClipValues
  288.  
  289. // Write Method
  290. function DynLayerWrite(html) {
  291. if (is.ns) {
  292. this.doc.open()
  293. this.doc.write(html)
  294. this.doc.close()
  295. }
  296. else if (is.ie) {
  297. this.event.innerHTML = html
  298. }
  299. }
  300. DynLayer.prototype.write = DynLayerWrite
  301.  
  302. // BrowserCheck Object
  303. function BrowserCheck() {
  304. var b = navigator.appName
  305. if (b=="Netscape") this.b = "ns"
  306. else if (b=="Microsoft Internet Explorer") this.b = "ie"
  307. else this.b = b
  308. this.version = navigator.appVersion
  309. this.v = parseInt(this.version)
  310. this.ns = (this.b=="ns" && this.v>=4)
  311. this.ns4 = (this.b=="ns" && this.v==4)
  312. this.ns5 = (this.b=="ns" && this.v==5)
  313. this.ie = (this.b=="ie" && this.v>=4)
  314. this.ie4 = (this.version.indexOf('MSIE 4')>0)
  315. this.ie5 = (this.version.indexOf('MSIE 5')>0)
  316. this.min = (this.ns||this.ie)
  317. }
  318. is = new BrowserCheck()
  319.  
  320. // CSS Function
  321. function css(id,left,top,width,height,color,vis,z,other) {
  322. if (id=="START") return '<STYLE TYPE="text/css">\n'
  323. else if (id=="END") return '</STYLE>'
  324. var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
  325. if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
  326. if (arguments.length>=5 && height!=null) {
  327. str += ' height:'+height+'px;'
  328. if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
  329. }
  330. if (arguments.length>=6 && color!=null) str += (is.ns)? ' layer-background-color:'+color+';' : ' background-color:'+color+';'
  331. if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
  332. if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
  333. if (arguments.length==9 && other!=null) str += ' '+other
  334. str += '}\n'
  335. return str
  336. }
  337. function writeCSS(str,showAlert) {
  338. str = css('START')+str+css('END')
  339. document.write(str)
  340. if (showAlert) alert(str)
  341. }
  342.